package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.entity.dto.SetmealDishDto;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.exception.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetMealMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired(required = false)
    private SetMealMapper setMealMapper;

    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;

    @Autowired(required =  false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishMapper dishMapper;

    /**
     * 保存套餐功能
     * @param setmealDto
     */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void save(SetmealDto setmealDto) {
        //补全信息
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());

        setMealMapper.save(setmealDto);

        //拿到套餐里的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //利用stream流补全套餐里的菜品信息
        List<SetmealDish> setmealDishList = setmealDishes.stream().map(setmealDish -> {
            //sort一定要加,要不然会报空指针异常
            setmealDish.setSort(0);
            //补全信息
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            return setmealDish;
        }).collect(Collectors.toList());

        //循环添加信息
        setmealDishMapper.batchSave(setmealDishList);
    }

    /**
     * 分页查询功能
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R<Page<SetmealDto>> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Setmeal> setmealList = setMealMapper.findByName(name);
        PageInfo<SetmealDish> pageInfo = new PageInfo<>();

        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryMapper.findById(categoryId);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        Page<SetmealDto> pageResult = new Page<>(setmealDtoList, pageInfo.getTotal());
        return R.success(pageResult);
    }

    /**
     * 根据ids删除选中的套餐
     * @param ids 选中的套餐,可以是多个
     */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void deleteByIds(List<Long> ids) {
        //查找选中的套餐是否在售
        Long count = setMealMapper.selectCount(ids);

        //判断,如果搜索到的套餐其中只要有一个套装在售,则无法删除
        if (count>0){
            throw new CustomerException("此套餐还在卖,你删不了");
        }

        //删除套餐
        setMealMapper.deleteByIds(ids);

        //删除套餐里包含的菜品
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 批量启售和停售功能
     * @param status
     * @param ids
     * @return
     */
    @Override
    public void changeStatus(Integer status, List<Long> ids) {
        setMealMapper.changeStatus(status,ids);
    }

    /**
     * 修改套餐功能
     * @param setmealDto
     * @return
     */
    @Override
    public void update(SetmealDto setmealDto) {
        //修改时间
        setmealDto.setUpdateTime(LocalDateTime.now());
        setMealMapper.updateById(setmealDto);

        setmealDishMapper.deleteBySetmealId(setmealDto.getId());

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //判断是否查到数据
        if (setmealDishes.size() == 0){
            return;
        }

        List<SetmealDish> setmealDishList = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setSort(0);
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishMapper.batchSave(setmealDishList);

    }

    /**
     * 根据套餐ID查询套餐
     * @param setmealId
     * @return
     */
    @Override
    public R<SetmealDto> findById(Long setmealId) {
        Setmeal setmeal = setMealMapper.findById(setmealId);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        List<SetmealDish> setmealDishList = setmealDishMapper.findBySetmealId(setmealId);
        setmealDto.setSetmealDishes(setmealDishList);
        return R.success(setmealDto);
    }

    /**
     * 移动端查看套餐
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status")
    public List<Setmeal> list(Long categoryId, Integer status) {

        List<Setmeal> setmealList = setMealMapper.list(categoryId,status);
        return setmealList;
    }

    /**
     * 移动端查看套餐详情
     * @param id
     * @return
     */
    @Override
    public R<List<SetmealDishDto>> findBySId(Long id) {
        List<SetmealDish> setmealDishList = setmealDishMapper.findBySetmealId(id);

        List<SetmealDishDto> setmealDishDtoList = setmealDishList.stream().map(setmealDish -> {

            Long dishId = setmealDish.getDishId();


            Dish dish = dishMapper.findById(dishId);

            SetmealDishDto setmealDishDto = new SetmealDishDto();
            BeanUtils.copyProperties(setmealDish, setmealDishDto);

            //获取菜品图片
            String image = dish.getImage();
            setmealDishDto.setImage(image);
            return setmealDishDto;
        }).collect(Collectors.toList());

        return R.success(setmealDishDtoList);
    }


}
